package com.yunkeji.config.keymonitor.task;

import com.yunkeji.api.user.mapper.ApiAppUserMapper;
import com.yunkeji.common.constant.CacheConstants;
import com.yunkeji.common.enums.app.EnableEnum;
import com.yunkeji.config.keymonitor.config.RedisListenerFactory;
import com.yunkeji.config.keymonitor.config.RedisListenerStrategy;
import com.yunkeji.dao.domain.AppUserVip;
import com.yunkeji.dao.mapper.AppUserVipMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author 老张
 * @version V1.0
 * @description 会员过期
 * @date 2021/11/15 14:10
 * @update 2021/11/15 14:10
 */
@Component
@Slf4j
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class MemberExclusiveExpire implements InitializingBean, RedisListenerStrategy {

    private final ApiAppUserMapper apiAppUserMapper;
    private final AppUserVipMapper appUserVipMapper;

    @Override
    public void afterPropertiesSet() throws Exception {
        RedisListenerFactory.put(CacheConstants.MEMBER_EXCLUSIVE_EXPIRE, this);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void execute(String keyInfo) {
        log.info("专属会员过期:{}", keyInfo);

        AppUserVip appUserVip =
                AppUserVip.builder()
                        .build()
                        .selectById(keyInfo.replace(CacheConstants.MEMBER_EXCLUSIVE_EXPIRE, ""));
        if (appUserVip != null) {
            AppUserVip build = AppUserVip.builder()
                .id(appUserVip.getId())
                .exclusiveVipFlag(EnableEnum.NO.getCode())
                .build();
            appUserVipMapper.updateById(build);
        }
    }
}
